package bn;

import main.Main;
import env.Road;

public class TerroristNode {
	public ChemReportedNode chemParentA;
	public ChemReportedNode chemParentB;
	public ArmyNode armyParentA;
	public ArmyNode armyParentB;
	public Road road;
	public static final int chemA = 1,chemB=2,armyA=4,armyB=8;
	public double p[];
	public TerroristNode(Road road)
	{
		this.road = road;
		chemParentA=road.getE1().bChemNode.son; //TODO potential null.
		chemParentB=road.getE2().bChemNode.son;
		armyParentA=road.getE1().bArmyNode;
		armyParentB=road.getE2().bArmyNode;
		p = new double[16];
		p[0] = 0.1;
		recalcP();
	}
	public void recalcP()
	{
		
		double negCalcCauseArmy = negCalcCause(Main.pArmyCause);
		double negCalcCauseChem = negCalcCause(Main.pChemCause);
		p[chemA]=calcCause(Main.pChemCause);//1-(1-(Pchem_cause)=Pchem_cause
		p[chemB]=calcCause(Main.pChemCause); 
		p[armyA]=calcCause(Main.pArmyCause);
		p[armyB]=calcCause(Main.pArmyCause);
		p[armyA+armyB]=1-(negCalcCauseArmy*negCalcCauseArmy);
		p[chemA+chemB]=1-(negCalcCauseChem*negCalcCauseChem);
		p[armyB+chemB]=1-(negCalcCauseArmy*negCalcCauseChem);
		p[armyA+chemA]=1-(negCalcCauseArmy*negCalcCauseChem);
		p[armyA+chemB]=1-(negCalcCauseArmy*negCalcCauseChem);
		p[armyB+chemA]=1-(negCalcCauseArmy*negCalcCauseChem);
		p[armyB+chemA+chemB]=1-(negCalcCauseArmy*negCalcCauseChem*negCalcCauseChem);
		p[armyA+chemA+chemB]=1-(negCalcCauseArmy*negCalcCauseChem*negCalcCauseChem);
		p[armyB+armyA+chemA]=1-(negCalcCauseArmy*negCalcCauseArmy*negCalcCauseChem);
		p[armyB+armyA+chemB]=1-(negCalcCauseArmy*negCalcCauseArmy*negCalcCauseChem);
		p[chemA+chemB+armyA+armyB]=1-(negCalcCauseArmy*negCalcCauseArmy*negCalcCauseChem*negCalcCauseChem);
		
	}
	private double calcCause(double p_cause)
	{
		return p_cause * Math.max(((double) (5 - road.getCost()))/5, 0.3);
	}
	private double negCalcCause(double p_cause)
	{
		return 1-calcCause(p_cause);
	}
	
	
	public String output()
	{
		String ans = "Road: " + road.getE1().getName()+ "<->" +road.getE2().getName()+ " ,weight: " + road.getCost() + "\n";
		ans+= "P(terrorrists  | not army 1, not army 2, not chem reported 1, not chem reported 2) = " + p[0] + "\n";
		ans+= "P(terrorrists  | not army 1, not army 2, chem reported 1, not chem reported 2) = " + round(p[1], 2) + "\n";
		ans+= "P(terrorrists  | not army 1, not army 2, not chem reported 1, chem reported 2) = " + round(p[2], 2) + "\n";
		ans+= "P(terrorrists  | not army 1, not army 2, chem reported 1, chem reported 2) = " + round(p[3], 2) + "\n";
		ans+= "P(terrorrists  | army 1, not army 2, not chem reported 1, not chem reported 2) = " + round(p[4], 2) + "\n";
		ans+= "P(terrorrists  | army 1, not army 2, chem reported 1, not chem reported 2) = " + round(p[5], 2) + "\n";
		ans+= "P(terrorrists  | army 1, not army 2, not chem reported 1, chem reported 2) = " + round(p[6], 2) + "\n";
		ans+= "P(terrorrists  | army 1, not army 2, chem reported 1, chem reported 2) = " + round(p[7], 2) + "\n";
		ans+= "P(terrorrists  | not army 1, army 2, not chem reported 1, not chem reported 2) = " + round(p[8], 2) + "\n";
		ans+= "P(terrorrists  | not army 1, army 2, chem reported 1, not chem reported 2) = " + round(p[9], 2) + "\n";
		ans+= "P(terrorrists  | not army 1, army 2, not chem reported 1, chem reported 2) = " + round(p[10], 2) + "\n";
		ans+= "P(terrorrists  | not army 1, army 2, chem reported 1, chem reported 2) = " + round(p[11], 2) + "\n";
		ans+= "P(terrorrists  | army 1, army 2, not chem reported 1, not chem reported 2) = " + round(p[12], 2) + "\n";
		ans+= "P(terrorrists  | army 1, army 2, chem reported 1, not chem reported 2) = " + round(p[13], 2) + "\n";
		ans+= "P(terrorrists  | army 1, army 2, not chem reported 1, chem reported 2) = " + round(p[14], 2) + "\n";
		ans+= "P(terrorrists  | army 1, army 2, chem reported 1, chem reported 2) = " + round(p[15], 2) + "\n";
		return ans;
	}
	
	static double round(double value, int places) {
	    if (places < 0) throw new IllegalArgumentException();

	    long factor = (long) Math.pow(10, places);
	    value = value * factor;
	    long tmp = Math.round(value);
	    return (double) tmp / factor;
	}
	public double getP(int sample)
	{
		
		return p[sample];
	}
}
